#include <iostream>
#include <vector>

using namespace std;
class Solution
{
public:
    double calculateTax(vector<vector<int>> &brackets, int income)
    {
        int ans = 0;
        int pre = 0;
        for (int i = 0; i < brackets.size(); i++)
        {
            if (income == 0)
            {
                break;
            }
            /* 当前区间的纳税值 * 税率 */
            int cur = min(brackets[i][0] - pre, income);
            ans += cur * brackets[i][1];
            income -= cur;
            pre = brackets[i][0];
        }
        return (double)ans / 100.0;
    }

    double calculateTax_2(vector<vector<int>> &brackets, int income)
    {
        double tax = 0.0;
        int i = 0, bracketsLen = brackets.size();
        while (i < bracketsLen )
        {
            if (income > brackets[i][0])
            {

                tax += (i == 0) ? (brackets[i][0] * brackets[i][1]) : ((brackets[i][0] - brackets[i - 1][0]) * brackets[i][1]);
            }
            else
            {
                tax += (i == 0) ? (income * brackets[i][1]) : ((income - brackets[i - 1][0]) * brackets[i][1]);
                break;
            }
            i++;
        }
        return tax * 0.01;
    }
};
int main()
{
    Solution s;
    vector<vector<int>> brackets = {{1, 0}, {4, 25}, {5, 50}};
    int income = 2;
    cout << s.calculateTax_2(brackets, income) << endl;
    system("pause");
    return 0;
}
